package com.ssii.www.myproject.query;

import com.github.wenhao.jpa.Specifications;
import com.ssii.www.myproject.domain.System;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
public class SystemQuery extends BaseQuery {

  //系统标识
  private String identification;
  //名字
  private String name;
  //编码
  private String scode;
  //版本
  private String version;

  /**
   * 创建spec对象
   * @return
   */
  @Override
  public Specification createSpec() {
//    return new Specification<System>() {
//      @Override
//      public Predicate toPredicate(Root<System> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//        Map<String, String> map = new HashMap<>(4);
//        map.put("identification", identification);
//        map.put("name", name);
//        map.put("scode", scode);
//        map.put("version",version);
//
//        List<Predicate> list = new ArrayList<>();
//        Predicate[] a = {};
//
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//          if (org.apache.commons.lang.StringUtils.isNotBlank(entry.getValue())) {
//            Path<String> objectPath = root.get(entry.getKey());
//            Predicate like = criteriaBuilder.like(objectPath, "%" + entry.getValue() + "%");
//            list.add(like);
//          }
//        }
//        for (int i = 0; i < list.size(); i++) {
//          a[i] = list.get(i);
//        }
//        CriteriaQuery<?> query = criteriaQuery.where(a);
//        Predicate restriction = query.getRestriction();
//        return restriction;
//      }
//    };
    Specification<System> specification = Specifications.<System>and()
      .like(StringUtils.isNotBlank(name),"name", "%"+name+"%")
      .like(StringUtils.isNotBlank(scode),"scode", "%"+scode+"%")
      .like(StringUtils.isNotBlank(version),"version", "%"+version+"%")
      .like(StringUtils.isNotBlank(identification),"identification", "%"+identification+"%")
      .build();
    return specification;
  }
  
}
